﻿using BarrageGameAssistant.Core.Configs;

namespace BarrageGameAssistant.UIForms
{
    public partial class FrmOpenGame : Form
    {
        /// <summary>
        /// 程序配置
        /// </summary>
        public Settings? Settings { get; set; }

        /// <summary>
        /// 游戏配置
        /// </summary>
        public GameProject? Project { get; private set; }

        public FrmOpenGame()
        {
            InitializeComponent();
        }

        private void FrmOpenGame_Load(object sender, EventArgs e)
        {

        }

        private void FrmOpenGame_Shown(object sender, EventArgs e)
        {
            try
            {
                if (Settings != null)
                {
                    string[] directories = Directory.GetDirectories(Settings.GameDirectory);

                    foreach (string directory in directories)
                    {
                        string gamePrj = Path.Combine(directory, "project.json");

                        var project = GameProject.Load(gamePrj);
                        if (project != null)
                        {
                            ListViewItem lvi = this.listViewEx1.Items.Add((this.listViewEx1.Items.Count + 1).ToString());
                            lvi.Tag = project;
                            lvi.SubItems.Add(project.GetPlatformName());
                            lvi.SubItems.Add(project.Name);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生错误：" + ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.listViewEx1.SelectedItems.Count > 0)
            {
                ListViewItem item = this.listViewEx1.SelectedItems[0];
                Project = item.Tag as GameProject;

                this.DialogResult = DialogResult.OK;
            }
        }
    }
}
